package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.GetUserEvent;
	import net.optix.ip.model.ModelLocator;
	import mx.controls.Alert;

	public class CheckLoginCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();


		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			var delegate:UserDelegate	=	new UserDelegate();
			delegate.checkLogin(this);
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{
			trace(event.result+"   ");
			//Alert.show(event.result,"check login");
			try
			{
				var data:XML	=	event.result;
				
				model.role		=	data.Role;
				model.userID	=	data.UserID;
				
				if(model.role > 0 && model.userID > 0)
				{
					var getUser:GetUserEvent	=	new GetUserEvent(model.userID);
					com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getUser);
				}
			}
			catch(error:Error)
			{
				trace(error.getStackTrace());
			}
//			var eve0:GetInventionsListEvent = new GetInventionsListEvent();
//			try
//			{
//				model.currentUser	=	event.result;
//				model.role			=	model.currentUser.Role;
//				CairngormEventDispatcher.getInstance().dispatchEvent(eve0);
//			}
//			catch(error:Error)
//			{
//				model.role			=	0;
//			}
//			if(model.role > 0)
//			{
//				
//			}

		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			trace(event.fault);
			model.role			=	0;
			model.result		=	"";
		}

	}
}